#!/usr/bin/env node

const fs = require("fs-extra");
const path = require("path");
const meow = require("meow");
const camelcase = require("camelcase");
const decamelize = require("decamelize");

const cli = meow(`
    Usage:
      $ ./scripts/create-new-module <module>
    Examples:
      $ ./scripts/create-new-module clone
`);

if (!cli.input.length) cli.showHelp();
let name = cli.input[0];

// Normalize module name
// turf-clone => clone
name = name.replace(/turf-/, "");
const camelcaseName = camelcase(name);
const decamelizeName = decamelize(name);

// Create Folder
const folderPath = path.join(
  __dirname,
  "..",
  "packages",
  `turf-${decamelizeName}`
);
if (!fs.existsSync(folderPath)) {
  fs.mkdirSync(folderPath);
  fs.mkdirSync(path.join(folderPath, "test"));
  fs.mkdirSync(path.join(folderPath, "test/in"));
  fs.mkdirSync(path.join(folderPath, "test/out"));
}

// Create index.js
fs.writeFileSync(
  path.join(folderPath, "index.js"),
  `/**
 * <DESCRIPTION>
 *
 * @name ${camelcaseName}
 * @param {Geometry|Feature<any>} feature1 GeoJSON Feature or Geometry
 * @param {Geometry|Feature<any>} feature2 GeoJSON Feature or Geometry
 * @returns {Boolean} true/false
 * @example
 * <SIMPLE EXAMPLE>
 */
export default function ${camelcaseName}(feature1, feature2) {
    return true;
};
`
);

// Create package.json
fs.writeFileSync(
  path.join(folderPath, "package.json"),
  `{
  "name": "@turf/${decamelizeName}",
  "version": "5.0.0",
  "description": "turf ${decamelizeName} module",
  "main": "main.js",
  "module": "main.es.js",
  "types": "index.d.ts",
  "files": [
    "index.js",
    "index.d.ts",
    "main.js",
    "main.es.js"
  ],
  "scripts": {
    "pretest": "rollup -c ../../rollup.config.js && echo '{"type":"module"}' > dist/es/package.json",
    "test": "node -r @std/esm test.js",
    "bench": "node -r @std/esm bench.js",
    "docs": "node ../../scripts/generate-readmes"
  },
  "repository": {
    "type": "git",
    "url": "git://github.com/Turfjs/turf.git"
  },
  "keywords": [
    "turf",
    "${name}"
  ],
  "author": "Turf Authors",
  "contributors": [
    "YOUR NAME <@GITHUB NAME>"
  ],
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/Turfjs/turf/issues"
  },
  "homepage": "https://github.com/Turfjs/turf",
  "devDependencies": {
    "@std/esm": "*",
    "benchmark": "*",
    "rollup": "*",
    "write-json-file": "*",
    "load-json-file": "*",
    "tape": "*"
  },
  "dependencies": {
    "@turf/helpers": "*"
  },
  "@std/esm": {
    "esm": "js",
    "cjs": true
  }
}
`
);

// Create LICENSE
const license = path.join(__dirname, "..", "packages", "turf", "LICENSE");
fs.copySync(license, path.join(folderPath, "LICENSE"));

// Create index.d.ts
fs.writeFileSync(
  path.join(folderPath, "index.d.ts"),
  `import { Feature, GeometryObject } from '@turf/helpers'

/**
 * http://turfjs.org/docs/#${name.toLocaleLowerCase().replace("-", "")}
 */

export default function (
    feature1: Feature<any> | GeometryObject,
    feature2: Feature<any> | GeometryObject
 ): boolean
`
);

// Create bench.js
fs.writeFileSync(
  path.join(folderPath, "bench.js"),
  `import Benchmark from 'benchmark';
import ${camelcaseName} from '.';
import glob from 'glob';
import path from 'path';
import load from 'load-json-file';

/**
 * Benchmark Results
 *
 * <Place results here>
 */
const suite = new Benchmark.Suite('turf-${decamelizeName}');
glob.sync(path.join(__dirname, 'test', 'in', '*.geojson')).forEach(filepath => {
    const {name} = path.parse(filepath);
    const geojson = load.sync(filepath);
    const [feature1, feature2] = geojson.features;
    console.time(name);
    ${camelcaseName}(feature1, feature2);
    console.timeEnd(name);
    suite.add(name, () => ${camelcaseName}(feature1, feature2));
});

suite
    .on('cycle', e => console.log(String(e.target)))
    .on('complete', () => {})
    .run();
`
);

// Create test.js
fs.writeFileSync(
  path.join(folderPath, "test.js"),
  `import test from 'tape';
import glob from 'glob';
import path from 'path';
import load from 'load-json-file';
import write from 'write-json-file';
import ${camelcaseName} from '.';

test('turf-${decamelizeName}', t => {
  glob.sync(path.join(__dirname, 'test', 'in', '*.json')).forEach(filepath => {
    // Define params
    const {name} = path.parse(filepath);
    const geojson = load.sync(filepath);
    // etc.

    const results = [];
    const out = filepath.replace(path.join('test', 'in'), path.join('test', 'out'));
    if (process.env.REGEN) write.sync(out, results);
    t.deepEqual(results, load.sync(out), name);
  });
  t.end();
});
`
);

// Create README.md
fs.writeFileSync(
  path.join(folderPath, "README.md"),
  `# @turf/${decamelizeName}

# ${camelcaseName}

<DESCRIPTION>

**Parameters**
<PARAMETERS>

**Examples**

<!-- This file is automatically generated. Please don't edit it directly:
if you find an error, edit the source file (likely index.js), and re-run
./scripts/generate-readmes in the turf project. -->

---

This module is part of the [Turfjs project](http://turfjs.org/), an open source
module collection dedicated to geographic algorithms. It is maintained in the
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
PRs and issues.

### Installation

Install this module individually:

\`\`\`sh
$ npm install @turf/${decamelizeName}
\`\`\`

Or install the Turf module that includes it as a function:

\`\`\`sh
$ npm install @turf/turf
\`\`\`
`
);
